package com.fido.kingkongview

import android.content.res.Resources
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.fido.kingkongview.holder.KHolder
import com.fido.kingkongview.utils.easyKHolderCreator

class MainActivity : AppCompatActivity() {

    private lateinit var kkView: KingKongView<String>
    private lateinit var kkView2: KingKongView<String>
    private lateinit var kkView3: KingKongView<String>
    private lateinit var btChangeOffscreen: Button
    private var list  = mutableListOf<String?>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findView()

        initEvent()

        for (i in (0 until 9)) {
            list.add("test${i}")
        }

        setupView()
    }

    private fun setupView() {
        changeStyle(kkView,1)
        changeStyle(kkView2,2)
        changeStyle(kkView3,0)
    }

    private fun initEvent() {
        btChangeOffscreen.setOnClickListener {
            itemOffScreenWidth = if (itemOffScreenWidth == 120) {
                0
            }else{
                120
            }
            btChangeOffscreen.text = "当前OffScreenWidth:${itemOffScreenWidth},重置为${if (itemOffScreenWidth==0) 120 else 0}"
            setupView()
        }
    }

    private var itemOffScreenWidth = 120
    private fun changeStyle(kkView:KingKongView<String>,index:Int){
        kkView.setUp(
            list = this.list,
            style = if (index == 0) KingKongStyle.FAKE_ALI else (if (index == 1) KingKongStyle.ONE_LINE else KingKongStyle.MULTI_LINE),
            lines = if (index>1) 2 else KingKongView.fakeAliRealLines(this.list,4),
            span = 4,
            itemOffScreenWidth = itemOffScreenWidth,
            itemClick = {data, pos->
                Toast.makeText(kkView.context,"pos=${pos} ${data}",Toast.LENGTH_SHORT).show()
            },
//            holderCreator = object :KHolderCreator<String>{
//                override fun creatorHolder(itemView: View): KHolder<String> {
//                    return MyHolder(itemView)
//                }
//                override fun getLayoutId() = R.layout.item_nav_list
//            }
            holderCreator = easyKHolderCreator(R.layout.item_list) { itemView, holder, data, position ->
                itemView.findViewById<TextView>(R.id.tv_text).text = data.toString()
                itemView.findViewById<ImageView>(R.id.iv_icon).setImageResource(R.mipmap.icon_jobfair)
            }
        )
        kkView.setupIndicator(if (index == 0) KingKongStyle.FAKE_ALI else (if (index == 1) KingKongStyle.ONE_LINE else KingKongStyle.MULTI_LINE),
            indicatorMarginTop = 10.dp)
    }

    class MyHolder(itemView: View) : KHolder<String>(itemView) {
        private var text:TextView?=null
        private var icon:ImageView?=null
        override fun initView(itemView: View) {
            text = itemView.findViewById(R.id.tv_text)
            icon = itemView.findViewById(R.id.iv_icon)
        }

        override fun onBind(holder: KHolder<String>, data: String?, position: Int) {
            text?.text  = data.toString()
            icon?.setImageResource(R.mipmap.icon_jobfair)
        }
    }


    private fun findView() {
        kkView = findViewById(R.id.kkView)
        kkView2 = findViewById(R.id.kkView2)
        kkView3 = findViewById(R.id.kkView3)
        btChangeOffscreen = findViewById(R.id.bt_changeOffScreen)
    }

}

internal val Number.dp
    get() = (this.toFloat() * Resources.getSystem().displayMetrics.density).toInt()